package com.suning.sawp.web.controller;

import java.security.Principal;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.suning.sawp.constants.ErrorCodeConstants;
import com.suning.sawp.constants.LoggerConstants;
import com.suning.sawp.dto.common.QueryResult;
import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.dto.promotion.HorizProRtn;
import com.suning.sawp.dto.promotion.ValidHorizProCouponRecord;
import com.suning.sawp.intf.promotion.HorizPromotionService;
import com.suning.sawp.service.util.BaseContants;
import com.suning.sawp.web.exception.MyGsonView;

/**
 * 
 * 异业促销活动查询controller<br>
 * 〈功能详细描述〉
 * 
 * @author 14062606
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Controller
@RequestMapping("/horizPro")
public class HorizPromotionController extends DjBaseController{
	
	private static final Logger LOGGER = LoggerFactory.getLogger(LoggerConstants.SAWP_SERVICE);

	@Autowired
	HorizPromotionService horizPromotionService;
	
	/**
     * 
     * 功能描述: 根据异业券号查询促销活动名称<br>
     * 
     * @param horizId 异业券号
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
	@RequestMapping("/queryHorizProActName.do")
	public MyGsonView queryHorizProActName(String horizId) {
		LOGGER.debug("开始根据异业券号查询促销活动名称,horizId="+horizId);
		ReturnMsg<HorizProRtn> rtn = new ReturnMsg<HorizProRtn>();
		try {
			rtn = horizPromotionService.queryHorizProActName(horizId);
		} catch (Exception e) {
			LOGGER.error("根据异业券号查询促销活动名称失败,horizId="+horizId+"error", e);
			String errorCode = ErrorCodeConstants.ERR_0002;
			rtn.setError(errorCode);
		}
		return setGsonView(rtn);
	}
	
	/**
     * 
     * 功能描述: 异业券验券<br>
     * 
     * @param mobile 手机号
     * @param horizId 异业券
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
	@RequestMapping("/validateHorizCoupon.do")
	public MyGsonView validateHorizCoupon(String mobile, String horizId, Principal principal) {
		String staffId = principal.getName();
		LOGGER.debug("开始淘宝异业券验券,mobile="+mobile+",horizId="+horizId+",staffId="+staffId);
		ReturnMsg<String> ret = new ReturnMsg<String>();
		try {
			ret = horizPromotionService.validateHorizCoupon(mobile, horizId, staffId);
		} catch (Exception e) {
			ret.setError(ErrorCodeConstants.ERR_0002);
			LOGGER.error("淘宝异业券验券失败,mobile="+mobile+",horizId="+horizId+",staffId="+staffId+",error="+e);
		}
		return setGsonView(ret);
	}
	
	/**
     * 
     * 功能描述: 分页查询验券（异业券）记录<br>
     * 〈功能详细描述〉
     *
     * @param principal
     * @param startIndex
     * @param limit
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/queryValidCouponRecord.do")
    public MyGsonView queryValidCouponRecord(Principal principal, int startIndex, int limit) {
        String staffId = principal.getName();
        MyGsonView view = createGsonView();
        QueryResult<ValidHorizProCouponRecord> result = horizPromotionService
                .queryHorizProCouponByStaffId(staffId, startIndex, limit);
        view.addStaticAttribute(BaseContants.DATA, result);
        
        return view;
    }
}
